﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CarbonExchange.Bll;

public partial class UserControls_CarbonFootprintQuestion_Shopping_HouseholdShopping : CarbonExchange.WebControls.AuditQuestionSigleItemBase
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public override void DataBind()
    {
        if (Audit.Country != null)
        {
            lblCurrency.Text = Audit.Country.CurrencySymbol;
        }
        txt.Text = Answer.GetSetting<string>("txt");
        base.DataBind();
    }

    protected override bool SaveQuestion()
    {
        Answer.SetSetting<string>("txt", txt.Text);

        var profileItemFactory = new AmeeProfileItemFactory();
        var dataCategoryFactory = new AmeeDataCatagoryFactory();

        var tmpProfileItem = EnsureProfileItem(Answer.SingleItem.Name, profileItemFactory, dataCategoryFactory, "/personal/generic", "type=household_personal");

        decimal value = -1;
        try
        {
            value = decimal.Parse(txt.Text);
        }
        catch (FormatException) { }

            value = value * Audit.Country.Conversion;

        var save = string.IsNullOrEmpty(tmpProfileItem.Id);

        if (value >= 0)
        {
            if (tmpProfileItem.GetValue<decimal>("currencyGBPPerMonth") != value)
            {
                tmpProfileItem.SetValue("currencyGBPPerMonth", value);
                save = true;
            }

            if (save)
            {
                profileItemFactory.Save(tmpProfileItem);
            }
        }

        Answer.SetSingleItem(tmpProfileItem);

        return value >= 0;
    }
}
